package com.dao.impl;

import com.dao.AddressDao;
import com.pojo.TbAddress;
import com.utils.DBUtils;
import com.utils.SqlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddressDaoImpl implements AddressDao {

    private QueryRunner queryRunner=new QueryRunner(DBUtils.getSource());

    @Override
    public List<TbAddress> findAllAddresByUid(long uid) {
        String sql = SqlUtils.getSql("address.selectbyuid");
        try {
            List<TbAddress> addressList = queryRunner.query(sql, new BeanListHandler<>(TbAddress.class), uid);
            return addressList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    public void createAddress(TbAddress tbadd) {
        String sql = "INSERT INTO `tb_address`(`detail`, `name`, `phone`, `uid`)" +
                " VALUES (?,?,?,?)";
        try {
            queryRunner.insert(sql, new BeanHandler<>(TbAddress.class), tbadd.getDetail(), tbadd.getName(), tbadd.getPhone(), tbadd.getUid());
            System.err.println("添加地址成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    @Override
    public List<TbAddress> showAddress(Long id) {
        String sql = "SELECT * FROM `tb_address` WHERE uid=?";
        List<TbAddress> query = null;
        try {
            query = queryRunner.query(sql, new BeanListHandler<>(TbAddress.class), id);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();

    }


    @Override
    public TbAddress selAddress(int id) {
        String sql = "SELECT * FROM `tb_address` WHERE id=?";
        try {
            TbAddress address = queryRunner.query(sql, new BeanHandler<>(TbAddress.class), id);
            return address;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new TbAddress();
    }


    @Override
    public void update(TbAddress tbadd) {
        String sql = "UPDATE `tb_address` SET `detail` = ?, `name` = ?, `phone` = ? WHERE `id` = ?";
        try {
            queryRunner.update(sql,tbadd.getDetail(),tbadd.getName(),tbadd.getPhone(),tbadd.getId());
            System.err.println("更新成功");
            return;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.err.println("更新失败");
    }


    @Override
    public void delAdd(int aid) {
        String sql="DELETE FROM `tb_address` WHERE id=?";
        try {
            queryRunner.update(sql,aid);
            System.err.println("删除成功");
            return;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.err.println("删除失败");
    }
}

